package model
{
	import laya.events.Event;
	import laya.net.HttpRequest;
	import laya.ui.View;
	import laya.utils.Browser;
	
	import script.ViewManager;
	
	import utils.UtilTool;
	import utils.WaitingRespond;

	public class HttpRequestUtil
	{
		private static var _instance:HttpRequestUtil;
		
		public static var httpUrl:String = "http://127.0.0.1:8700/";//"http://47.111.13.238:8700/" ;//	"http://47.98.218.56/scfy/"; //"http://dhs3iy.natappfree.cc/";//
		
		//public static var httpUrl:String = "http://127.0.0.1:8090/";//"../scfy/";//http://www.cmyk.com.cn/scfy/" ;//	"http://47.98.218.56/scfy/"; //"http://dhs3iy.natappfree.cc/";//

		public static var registerUrl:String = "clientUser/addUser?";
		
		public static var getVerifyCode:String = "clientUser/getVerifyCode?";

		public static var loginInUrl:String = "clientUser/login?";
		public static var loginOutUrl:String = "clientUser/loginOut?";

		
		public static var getGoodsList:String = "goods/loadAllGoods?";

		public static var getUserCartList:String = "user-cart/getUserCartList?";
		public static var addGoodsToCart:String = "user-cart/addGoods?";
		public static var getCartGoodsCount:String = "user-cart/getUserCartCount?";
		public static var updateGoodCount:String = "user-cart/updateGoodCount?";
		public static var setUserAddress:String = "clientUser/updateAddress?";
		public static var confirmOrder:String = "order/confirmOrder?";

		public static var getClientUserOrderList:String = "order/getClientUserOrderList?";
		public static var getOrderDetail:String = "order/getUserOrderDetail?";
		public static var cancelOrder:String = "order/cancelOrder?";

		
		public static function get instance():HttpRequestUtil
		{
			if(_instance == null)
				_instance = new HttpRequestUtil();
			return _instance;
		}
		
		public function HttpRequestUtil()
		{
			
		}
		
		private function newRequest(url:String,caller:Object=null,complete:Function=null,param:Object=null,requestType:String="text",onProgressFun:Function = null,headers:Array=null):void{
			
			var request:HttpRequest=new HttpRequest();
			request.on(Event.PROGRESS, this, function(e:Object)
			{
				if(onProgressFun != null)
					onProgressFun(e);
			});
			request.on(Event.COMPLETE, this,onRequestCompete,[caller, complete,request]);
			
			//var self:HttpModel=this;
			function checkOver(url:String,caller:Object,complete:Function,param:Object,requestType:String,request:HttpRequest){
				onHttpRequestError(url,caller,complete,param,requestType,request);
			}
			Laya.timer.once(5000,request,checkOver,[url,caller,complete,param,requestType,request]);
			
			
			
			request.on(Event.ERROR, this, onHttpRequestError,[url,caller,complete,param,requestType,request]);
			if(param!=null){
				if(param is String){
					
				}else if(param is ArrayBuffer){
					
				}else{
					var query:Array=[];
					for(var k in param){
						
						query.push(k+"="+encodeURIComponent(param[k]));
					}
					param=query.join("&");
				}
			}
			console.log(url+param);
			request["retrytime"]=0;
			request.send(url, param, requestType?'post':'get', "text",headers);
		}
		
		private function onHttpRequestError(url:String,caller:Object,complete:Function,param:Object,requestType:String,request:HttpRequest,e:Object=null):void
		{
			//ViewManager.showAlert("您的网络出了个小差，请重试！");
		}
		
		private function onHttpRequestProgress(e:Object=null):void
		{
			trace(e)
		}
		
		private function onRequestCompete(caller:Object,complete:Function,request:HttpRequest,data:Object):void
		{
			WaitingRespond.instance.hideWaitingView();

			try
			{
				var result:Object = JSON.parse(data as String);
				if(result && result.hasOwnProperty("status"))
				{
					if(result.code != HttpErrorCode.OK)
					{
						ViewManager.showAlert(result.msg);
//						if(ErrorCode.ErrorTips.hasOwnProperty(result.status))
//						{
//							ViewManager.showAlert(ErrorCode.ErrorTips[result.status]);
//						}
//						if(result.status == 203 || result.status == 210)
//						{
//							ViewManager.instance.openView(ViewManager.VIEW_lOGPANEL,true);
//						}
					}
				}
			}
			catch(err:Error)
			{
				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);
				return;
			}
			Laya.timer.clearAll(request);
			// TODO Auto Generated method stub
			if(caller&&complete)complete.call(caller,data);
			request.offAll();
		}
		
		public  function Request(url:String,caller:Object=null,complete:Function=null,param:Object=null,type:String="get",onProgressFun:Function = null,showwaiting:Boolean=true,headers:Array=null):void{
			
			var logtime:String = UtilTool.getLocalVar("loginTime","");
			if(Userdata.instance.loginTime != 0 && logtime != Userdata.instance.loginTime.toString())
			{
				ViewManager.showAlert("不能同时登陆两个账号，请重新登录");
				Userdata.instance.isLogin = false;
				
				UtilTool.setLocalVar("useraccount","");
				UtilTool.setLocalVar("userpwd","");
				
				Browser.window.location.reload();
				//ViewManager.instance.openView(ViewManager.VIEW_FIRST_PAGE,true);
				
				return;
			}
			
			if(showwaiting)
			{
				WaitingRespond.instance.showWaitingView();
			}
			newRequest(url,caller,complete,param,type,onProgressFun,headers);
		}
		// --- Static Functions ------------------------------------------------------------------------------------------------------------------------------------ //
		public  function RequestBin(url:String,caller:Object=null,complete:Function=null,param:Object=null):void{
			newRequest(url,caller,complete,param,"arraybuffer");
		}
		
	}
}